package com.sunlg.smarthome.ui.main;

import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.Switch;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.BaseActivity;
import com.sunlg.smarthome.utils.JPushUtils;
import com.sunlg.smarthome.utils.PreferHelper;

import butterknife.BindView;
import cn.jpush.android.api.JPushInterface;

public class SettingActivity extends BaseActivity implements Switch.OnCheckedChangeListener{

    @BindView(R.id.switch_push)
    Switch switchPush;
    @BindView(R.id.rl_about)
    RelativeLayout rlAbout;

    private PreferHelper preferHelper;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void setupToolBar(Toolbar toolbar) {
        super.setupToolBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("设置");
        actionBar.setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doFinish();
            }
        });
    }

    @Override
    protected void init() {
        preferHelper = new PreferHelper(App.getInstance(), "setting");
    }

    @Override
    protected void initView() {
        switchPush.setOnCheckedChangeListener(this);
        rlAbout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overlay(AboutActivity.class);
            }
        });

        if(preferHelper.getBoolean("is_push", false)){
            switchPush.setChecked(true);
        }else {
            switchPush.setChecked(false);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()){
            case R.id.switch_push:
                preferHelper.setBoolean("is_push", isChecked);
                if(!isChecked){
                    JPushInterface.stopPush(this);
                }else {
                    JPushInterface.resumePush(this);
                }
        }
    }

}
